﻿Ext.define("Kwan.ex.TreeCombo", {
    extend: "Ext.form.field.ComboBox",
    //requires: ["Ext.tree.Panel"],
    labelAlign: 'right',
    emptyText: "请选择...",
    mode: 'local',
    queryMode: 'local',
    forceSelection: true,
    triggerAction: 'all',
    typeAhead: true,
    selectOnFocus: true,
    valueField: 'Value',
    displayField: 'Key',
    editable: false,
    initComponent: function () {
        var me = this;
        me.treeDS = me.store;
        var ex_data = [], nodes = me.store.tree.root.childNodes;
        Ext.each(nodes, function (node) {
            ex_data.push({ 'Key': node.data.text, 'Value': node.data.id });
        });
        this.store = Ext.create('Ext.data.Store', {
            fields: ['Key', 'Value'],
            data: ex_data,
            autoLoad: true
        });
        this.store.load();
        this.callParent(arguments);
    },
    createPicker: function () {
        var me = this;
        me.picker = Ext.create('Ext.tree.Panel', {
            height: 250,
            autoScroll: true,
            floating: true,
            focusOnToFront: false,
            shadow: true,
            ownerCt: this.ownerCt,
            useArrows: true,
            store: me.treeDS,
            rootVisible: false,
            listeners: {
                'itemclick': function (view, re) {
                    me.setRawValue(re.data.text); // 隐藏值
                    me.setValue(re.data.id); // 显示值
                    me.collapse();
                    //Kwan.Debug(me);
                    //alert(me.getValue() + "," + me.getRawValue());
                }
            }
        });
        me.picker.on({
            checkchange: function () {
                var records = me.picker.getView().getChecked(), names = [], values = [];
                Ext.Array.each(records, function (rec) {
                    names.push(rec.get('text'));
                    values.push(rec.get('id'));
                });
                me.setRawValue(values.join(';')); // 隐藏值
                me.setValue(names.join(';')); // 显示值
            }
        });
        return me.picker;
    },
    alignPicker: function () {
        var me = this, picker, isAbove, aboveSfx = '-above';
        if (this.isExpanded) {
            picker = me.getPicker();
            if (me.matchFieldWidth) {
                picker.setWidth(me.bodyEl.getWidth());
            }
            if (picker.isFloating()) {
                picker.alignTo(me.inputEl, "", me.pickerOffset); // ""->tl
                isAbove = picker.el.getY() < me.inputEl.getY();
                me.bodyEl[isAbove ? 'addCls' : 'removeCls'](me.openCls + aboveSfx);
                picker.el[isAbove ? 'addCls' : 'removeCls'](picker.baseCls + aboveSfx);
            }
        }
    }
});